package com.cash.channel.svc.service.common;

import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.LoanCalculateRequest;
import com.cash.channel.api.model.response.LoanCalculateResponse;
import com.cash.channel.svc.logic.ProductLogic;
import com.cash.channel.svc.service.base.LoanCalculate;
import geex.core.service.LoanService;
import geex.core.service.model.loan.TryCalculateRepayPlan;
import geex.core.service.model.loan.TryCalculateResult;
import geex.core.service.model.product.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.贷款试算)
public class CommonLoanCalculate implements LoanCalculate {

    @Autowired
    private ProductLogic productLogic;

    @Resource
    private LoanService loanService;

    @Override
    public ApiResponseModel<LoanCalculateResponse> doLoanCalculate(ApiRequestModel apiRequestModel) {
        try {
            LoanCalculateRequest loanCalculateRequest = JSON.parseObject(apiRequestModel.getData(), LoanCalculateRequest.class);
            Product product = productLogic.findProductById(loanCalculateRequest.getPeriod(), loanCalculateRequest.getProductCode(), apiRequestModel.getChannel());
            if (null != product) {
                log.info("核心试算：调用核心试算接口：" + product.getProductCode());
                TryCalculateResult tryCalculateResult = loanService.tryCalculate(product.getProductCode(), loanCalculateRequest.getAmount().doubleValue(), null);
                if (tryCalculateResult == null) {
                    throw new CashChannelException("试算出错!");
                }
                BigDecimal serviceFee = new BigDecimal(tryCalculateResult.getAllPayServiceCharge()).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal actualAmount = loanCalculateRequest.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal fee = new BigDecimal(tryCalculateResult.getAllPayFee() + tryCalculateResult.getAllPayServiceCharge()).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal repayAmt = new BigDecimal(tryCalculateResult.getAllPayAmount()).add(fee).setScale(2, BigDecimal.ROUND_HALF_UP);
                LoanCalculateResponse response = new LoanCalculateResponse();
                response.setActualAmount(actualAmount);
                response.setPayAmount(repayAmt);
                response.setPayFee(fee);
                response.setServiceFee(serviceFee);
                response.setRemark("本金" + actualAmount + "元，利息" + fee + "元");
                getRepayPlanByCalculateResult(tryCalculateResult, response);
                return new ApiResponseModel<>(true, 0, "", response);
            } else {
                throw new CashChannelException("未找到对应产品!");
            }
        } catch (CashChannelException e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getErrMessage(), null);
        }

    }

    private void getRepayPlanByCalculateResult(TryCalculateResult tryCalculateResult, LoanCalculateResponse response) {
        List<TryCalculateRepayPlan> tryCalculateRepayPlans=tryCalculateResult.getTryCalculateRepayPlanList();
        for (TryCalculateRepayPlan tryCalculateRepayPlan : tryCalculateRepayPlans) {
            response.addCalculateRepayPlan(tryCalculateRepayPlan.getTenor(), tryCalculateRepayPlan.getPayDate()
                    , BigDecimal.valueOf(tryCalculateRepayPlan.getPayAmount()).setScale(2), BigDecimal.valueOf(tryCalculateRepayPlan.getPayCorpus()).setScale(2)
                    , BigDecimal.valueOf(tryCalculateRepayPlan.getPayFee()).setScale(2), BigDecimal.valueOf(tryCalculateRepayPlan.getServiceCharge()).setScale(2));
        }
    }

}
